package servlet.medicalAppointment;

import  bean.pojo.Medical;
import com.alibaba.fastjson2.JSONObject;
import commons.baseServlet.BaseServlet;
import commons.format.NumberFormat;
import commons.r.R;
import service.MedicalAppointmentService;
import service.impl.MedicalAppointmentServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

/**
 * servlet.medicalAppointment
 * User: 李雨行
 * Date: 2024/12/16 15:56
 * motto:   沉淀哥
 * Description:
 * Version: V1.0
 */
@WebServlet(value = "/medicalAppointment/*")
public class MedicalAppointmentServlet extends BaseServlet {
    public R findByPage(HttpServletRequest request){
        //获取医疗预约id
        Integer appointmentId= NumberFormat.stringToInteger(request.getParameter("appointmentId"));
        Integer page= NumberFormat.stringToInteger(request.getParameter("page"));
        Integer limit= NumberFormat.stringToInteger(request.getParameter("limit"));
        Integer userId= NumberFormat.stringToInteger(request.getParameter("userId"));
        String medicalItem=request.getParameter("medicalItem");//""
        MedicalAppointmentService medicalAppointmentService=new MedicalAppointmentServiceImpl();
        List<Medical> list=medicalAppointmentService.findByPage(appointmentId,userId,medicalItem,page,limit);
        Integer maxPage=medicalAppointmentService.maxPage(appointmentId,userId,medicalItem,page,limit);
        return list.size()>0?R.ok().addData("total",maxPage).addData("data",list):R.error("没有数据");
    }

    public R add(HttpServletRequest request){
        try {
            BufferedReader bufferedReader=request.getReader();
            String data="";
            data=bufferedReader.readLine();
            Medical medical= JSONObject.parseObject(data,Medical.class);
            MedicalAppointmentService medicalAppointmentService=new MedicalAppointmentServiceImpl();
            boolean t=medicalAppointmentService.add(medical);
            return t?R.ok("添加成功"):R.error("添加失败");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return R.error("出错");
    }

    public R deleteById(HttpServletRequest request){
        Integer id=NumberFormat.stringToInteger(request.getParameter("appointmentId"));
        MedicalAppointmentService medicalAppointmentService=new MedicalAppointmentServiceImpl();
        boolean t=medicalAppointmentService.deleteById(id);
        return t?R.ok("删除成功"):R.error("删除失败");
    }
    public R update(HttpServletRequest request){

        try {
            BufferedReader bufferedReader=request.getReader();
            String data=bufferedReader.readLine();
            Medical medical=JSONObject.parseObject(data,Medical.class);
            MedicalAppointmentService medicalAppointmentService=new MedicalAppointmentServiceImpl();
            boolean t=medicalAppointmentService.update(medical);
            return t?R.ok("修改成功"):R.ok("修改失败");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return R.error("添加出错");
    }
}
